package com.scedu.controller;

import com.scedu.convert.OrderFomr2OrderDTOConverter;
import com.scedu.dto.OrderDTO;
import com.scedu.enums.ResultEnums;
import com.scedu.exception.SellException;
import com.scedu.form.DetailForm;
import com.scedu.form.ListForm;
import com.scedu.form.OrderForm;
import com.scedu.service.impl.BuyerServiceImpl;
import com.scedu.service.impl.OrderServiceImpl;
import com.scedu.utils.ResultUtil;
import com.scedu.viewobject.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
买家商品
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j //日志
public class BuyerOrderController {
    @Autowired
    OrderServiceImpl orderService;
    @Autowired
    BuyerServiceImpl buyerService;

    //创建订单
    //POST /sell/buyer/order/create
    @PostMapping("/create")
   // public Result create(){
    public Result<Map<String,String>> create(@Valid OrderForm orderForm, BindingResult bindingresult){



        if (bindingresult.hasErrors()){
            //有验证错误
            log.error("表单创建错误 ，参数错误 orderFomr={}" ,orderForm);
            //throw new SellException(ResultEnums.ORDERS_FORM_ERROR);
            throw new SellException(ResultEnums.ORDERS_FORM_ERROR.getCode(),
                    bindingresult.getFieldError().getDefaultMessage());
            //return ResultUtil.fail(100,bindingresult.getFieldError().getDefaultMessage());
            //return  null;
        }

            OrderDTO orderDTO= OrderFomr2OrderDTOConverter.convert(orderForm);
            if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
                log.error("购物车为空，string={}",orderDTO.getOrderDetailList());
                throw new SellException(ResultEnums.ORDER_ITEMS_NULL_ERROR);
            }
            OrderDTO resulto= orderService.create(orderDTO);
            String orderID=resulto.getOrderId();
             log.error("orderid={}",orderID);
            Map<String,String> map=new HashMap<>();
            map.put("orderId",orderID);


            return ResultUtil.success(map);



    }

    //查询订单列表
//    GET /sell/buyer/order/list
//    openid: 18eu2jwk2kse3r42e2e
//    page: 0 //从第0页开始
//    size: 10
    @GetMapping(value = "/list")
    public Result<List<OrderDTO>> list(@Valid ListForm listForm,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            //有验证错误
            log.error("表单列表参数错误 ，参数错误 listForm={}" ,listForm);
            //throw new SellException(ResultEnums.ORDERS_FORM_ERROR);
            throw new SellException(ResultEnums.ORDERS_LISTFORM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
            //return ResultUtil.fail(100,bindingresult.getFieldError().getDefaultMessage());
            //return  null;
        }
        //PageRequest pageRequest= PageRequest.of(0,5);
        PageRequest pageRequest= PageRequest.of(listForm.getPage(),listForm.getSize());
        //dae格式转时间戳
        Page<OrderDTO> resultRequest=  orderService.findList(listForm.getOpenid(),pageRequest);
        return ResultUtil.success(resultRequest.getContent());
    }

    //订单详情
    @GetMapping(value = "/detail")
    public Result<OrderDTO> detail(@RequestParam("openid") String openid,
                                   @RequestParam("orderId") String orderId){
        if (StringUtils.isEmpty(orderId)){
            //有验证错误
            log.error("表单参数错误 ，参数错误 listForm={}" ,orderId);
            //throw new SellException(ResultEnums.ORDERS_FORM_ERROR);
            throw new SellException(ResultEnums.ORDERS_LISTFORM_ERROR);
            //return ResultUtil.fail(100,bindingresult.getFieldError().getDefaultMessage());
            //return  null;
        }

        OrderDTO orderDTO=buyerService.findeOrderOne(openid, orderId);
        return ResultUtil.success(orderDTO);
    }


    //取消订单
    @PostMapping(value = "/cancel")
    public Result cancel (@RequestParam("openid") String openid,
                            @RequestParam("orderId") String orderId){
        if (StringUtils.isEmpty(orderId)){
            //有验证错误
            log.error("表单参数错误 ，参数错误 listForm={}" ,orderId);
            //throw new SellException(ResultEnums.ORDERS_FORM_ERROR);
            throw new SellException(ResultEnums.ORDERS_LISTFORM_ERROR);
            //return ResultUtil.fail(100,bindingresult.getFieldError().getDefaultMessage());
            //return  null;
        }
        OrderDTO orderDTO=buyerService.calcelOrder(openid, orderId);
        return ResultUtil.success();

    }
}
